
//管理员接口模板

const app = getApp();
const baseUrl = "https://yps.yikesong.cc/api";// yikesong.cc正式服
//const baseUrl = "https://hc.guyubao.com/api"; // 黄超测试服
//const baseUrl = "https://peter.xiaomiqiu.com/api"; // 小米球测试服
import base64 from 'base64';
const requestUser = function (createData) {
  return new Promise(function (resolve, reject) {
    //console.log('走接口数据了',createData);
    let isJsonrequest = false;
    let isPathParams = false;
    //判断json请求
    if (createData.data != undefined) {
      isJsonrequest = true;
    }
    //判断?连接请求
    let params = '?';
    if (createData.params != undefined) {
      isPathParams = true;
      for (let i in createData.params) {
        params += i;
        params += '=';
        params += createData.params[i];
        params += '&';
      }
      //获取最后一个&
      params = params.substring(0, params.length - 1);
    }

    //判断头信息
    let headers = {
      Accept: 'application/json',
      Authorization: 'Bearer ' + wx.getStorageSync("tokenObject").access_token,
      RefreAuthorization: wx.getStorageSync("tokenObject").refresh_token,
    };
   // console.log(headers)

    if (createData.headers != null) {
      headers = createData.headers;
    }
    //console.log(`${baseUrl + createData.url}` + (isPathParams ? params : ''))
    wx.request({
      url: `${baseUrl + createData.url}` + (isPathParams ? params : ''),
      data: JSON.stringify(createData.data),
      header: headers,
      method: createData.method,
      success: function (res) {
        //console.log(res)
        if (res.header.Authorization){
          let Base64 = base64.Base64;
          let tokenObject = wx.getStorageSync("tokenObject");
          tokenObject.access_token = res.header.Authorization;
          tokenObject.refresh_token = res.header.RefreAuthorization;
          wx.setStorageSync("tokenObject", tokenObject)
        }
        if (res.data.flag) {
          resolve(res);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        reject(res);
      },
      complete: function (res) { },
    })
  });
}


export default requestUser